package com.wangba.controller;

import com.wangba.pojo.User;
import com.wangba.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")

public class UserController {

    @Autowired
    private UserService us;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/loginUser", method = RequestMethod.POST)
    public String validateLogin(Model model, HttpSession session, String username, String password) {
        User user = us.Validate(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            return "redirect:/member/allMember";
        } else {
            model.addAttribute("errorMessage", "用户名或者密码错误");
            return "login";
        }
    }

}

